#include "Database.h"
#include "sqlite3.h"
#include <stdio.h>
#include <string.h>
#include <tchar.h>
#include "utility.h"

sqlite3* db;

#ifdef UNICODE
DB_API int InitDb(wchar_t* dbFileName)
#else
DB_API int InitDb(char* dbFileName)
#endif
{
#ifdef UNICODE
	char dbName[64] = {0};
	w2c(dbName, 64, dbFileName);
	return sqlite3_open(dbName, &db);
#else
	return sqlite3_open(dbFileName, &db);
#endif
}

DB_API int CloseDb()
{
	if(db != NULL)
	{
		sqlite3_close(db);
		db = NULL;
	}
	return 0;
}

#ifdef UNICODE
wchar_t* GetConfigByKey(wchar_t* key)//, wchar_t* value)
#else
char* GetConfigByKey(char* key)//, char* value)
#endif
{
	int rc;
	sqlite3_stmt* stmt;
	char sql[128] = {0};

#ifdef UNICODE
	wchar_t value[64] = {0};
	char k[32] = {0};
	w2c(k, 32, key);
	sprintf(sql, "select config.[value] from config where config.[key]='%s'", k);
#else
	char value[64] = {0};
	sprintf(sql, "select config.[value] from config where config.[key]='%s'", key);
#endif
	rc = sqlite3_prepare_v2(db, sql, 128, &stmt, 0);
	if(rc != SQLITE_OK)
	{
		return NULL;
	}
	rc = sqlite3_step(stmt);
	if(rc != SQLITE_ROW)
	{
		return NULL;
	}
#ifdef UNICODE
	char v[64] = {0};
	strcpy(v, (char*)sqlite3_column_text(stmt, 0));
	c2w(value, 64, v);
#else
	strcpy(value, (char*)sqlite3_column_text(stmt, 0));
#endif
	//return SQLITE_OK;
	return value;
}

DB_API int ListDesktop(Desktop desktop, int size)
{
	int rc;
	sqlite3_stmt* stmt;
	char sql[256] = "select desktop.[x],desktop.[y],desktop.[width],desktop.[height],desktop.[icon],desktop.[exec],desktop.[background] from desktop where desktop.[theme]='metro'";
	rc = sqlite3_prepare_v2(db, sql, 128, &stmt, 0);
	if(rc != SQLITE_OK)
	{
		return rc;
	}
	rc = sqlite3_step(stmt);

	int offset = 0;
	while(rc == SQLITE_ROW)
	{
		Page* p = desktop + offset;
		p->x = sqlite3_column_int(stmt, 0);
		p->y = sqlite3_column_int(stmt, 1);
		p->width = sqlite3_column_int(stmt, 2);
		p->height = sqlite3_column_int(stmt, 3);
		c2w(p->icon, 64, (char*)sqlite3_column_text(stmt, 4));
		c2w(p->exec, 64, (char*)sqlite3_column_text(stmt, 5));
		c2w(p->background, 16, (char*)sqlite3_column_text(stmt, 6));
		memset(p->icon, 0, 64);
		memset(p->exec, 0, 64);
		memset(p->background, 0, 16);

		offset++;
		if(offset >= size)
		{
			break;
		}
		rc = sqlite3_step(stmt);
	}

#ifdef UNICODE
#else
#endif
	return rc;
}

DB_API int GetDesktopCount()
{
	int rc;
	sqlite3_stmt* stmt;
	char sql[128] = "select count(0) from desktop";
	rc = sqlite3_prepare_v2(db, sql, 128, &stmt, 0);
	if(rc != SQLITE_OK)
	{
		return rc;
	}
	rc = sqlite3_step(stmt);
	if(rc != SQLITE_ROW)
	{
		return rc;
	}
	return sqlite3_column_int(stmt, 0);
}

DB_API int InitConfig(lpConfig config)
{
#ifdef UNICODE
	wchar_t* value = GetConfigByKey(L"current_theme");
	if(value != NULL)
	{
		wcscpy(config->current_theme, value);
	}
	value = GetConfigByKey(L"desktop_page");
	if(value != NULL)
	{
		wcscpy(config->desktop_page, value);
	}
	value = GetConfigByKey(L"margin_left");
	if(value != NULL)
	{
		config->margin_left = _wtoi(value);
	}
	value = GetConfigByKey(L"margin_top");
	if(value != NULL)
	{
		config->margin_top = _wtoi(value);
	}
	value = GetConfigByKey(L"unit_length");
	if(value != NULL)
	{
		config->unit_length = _wtoi(value);
	}
#else
	char* value = GetConfigByKey("current_theme");
	if(value != NULL)
	{
		strcpy(config->current_theme, value);
	}
	value = GetConfigByKey("desktop_page");
	if(value != NULL)
	{
		strcpy(config->desktop_page, value);
	}
	value = GetConfigByKey("margin_left");
	if(value != NULL)
	{
		config->margin_left = atoi(value);
	}
	value = GetConfigByKey("margin_top");
	if(value != NULL)
	{
		config->margin_top = atoi(value);
	}
	value = GetConfigByKey("unit_length");
	if(value != NULL)
	{
		config->unit_length = atoi(value);
	}
#endif
	return 0;
}